﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cesave.Domain.PermisosModule.Contracts.Estados;
using System.ComponentModel;
using Cesave.Infrastructure.CrossCutting.IoC;
using Cesave.Domain.PermisosModule.Entities;
using System.Windows.Forms;
using Cesave.DistributedServices.PermisosModule.WindowsHost.Resources;
using Cesave.Domain.Core.Entities;

namespace Cesave.DistributedServices.PermisosModule.WindowsHost.ViewModels.Catalogs.Estados
{
	public partial class EstadosViewModel
	{

		#region Private properties

		/// <summary>
		/// 
		/// </summary>
		private List<Estado> _estados;

		/// <summary>
		/// 
		/// </summary>
		private Estado _estado;

		#endregion

		#region Public properties

		/// <summary>
		/// 
		/// </summary>
		public List<Estado> Estados
		{
			get
			{
				return _estados;
			}
			private set
			{
				_estados = value;
				if (_estados != null)
					RaisePropertyChanged("Estados");
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public Estado Estado
		{
			get
			{
				return _estado;
			}
			private set
			{
				_estado = value;
				if (_estado != null)
					RaisePropertyChanged("Estado");
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public bool IsEstadoReady
		{
			get
			{
				return Estado != null;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public bool IsEstadoActive
		{
			get
			{
				return IsEstadoReady ? Estado.Status : false;
			}
		}

		#endregion

		#region Action methods

		/// <summary>
		/// 
		/// </summary>
		private void LoadEstados()
		{
			StartWaiting();
			using (BackgroundWorker worker = new BackgroundWorker())
			{
				worker.DoWork += (sender, e) =>
				{
					IEstadoService estadoService = IoCFactory.Resolve<IEstadoService>();
					e.Result = estadoService.FindAllEstados();
				};
				worker.RunWorkerCompleted += (sender, e) =>
				{
					EndWaiting();
					if (!e.Cancelled && e.Error == null)
						Estados = e.Result as List<Estado>;
					else if (e.Error != null)
						MessageBox.Show(
							_estadosView,
							e.Error.Message,
							WindowTitles.EstadosWindowTitle,
							MessageBoxButtons.OK,
							MessageBoxIcon.Error)
						;
				};
				worker.RunWorkerAsync();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ActivateEstado(object sender, EventArgs e)
		{
			StartWaiting();
			_estadosView.switchActivationButton.Image = null;
			_estadosView.switchActivationButton.Text = "Activando";
			_estadosView.switchActivationButton.Enabled = false;
			using (BackgroundWorker worker = new BackgroundWorker())
			{
				worker.DoWork += (wSender, wArgs) =>
				{
					IEstadoService estadoService = IoCFactory.Resolve<IEstadoService>();
					estadoService.ActivateEstado(Estado);
				};
				worker.RunWorkerCompleted += (wSender, wArgs) =>
				{
					EndWaiting();
					if (!wArgs.Cancelled && wArgs.Error == null)
						SetCurrentEstado(_estadosView.estadosBindingSource, new EventArgs());
					else
						MessageBox.Show(
							_estadosView,
							wArgs.Error.Message,
							WindowTitles.EstadosWindowTitle,
							MessageBoxButtons.OK,
							MessageBoxIcon.Error
						);
				};
				worker.RunWorkerAsync();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void DeactivateEstado(object sender, EventArgs e)
		{
			StartWaiting();
			_estadosView.switchActivationButton.Image = null;
			_estadosView.switchActivationButton.Text = "Desactivando";
			_estadosView.switchActivationButton.Enabled = false;
			using (BackgroundWorker worker = new BackgroundWorker())
			{
				worker.DoWork += (wSender, wArgs) =>
				{
					IEstadoService estadoService = IoCFactory.Resolve<IEstadoService>();
					estadoService.DeactivateEstado(Estado);
				};
				worker.RunWorkerCompleted += (wSender, wArgs) =>
				{
					EndWaiting();
					if (!wArgs.Cancelled && wArgs.Error == null)
						SetCurrentEstado(_estadosView.estadosBindingSource, new EventArgs());
					else
						MessageBox.Show(
							_estadosView,
							wArgs.Error.Message,
							WindowTitles.EstadosWindowTitle,
							MessageBoxButtons.OK,
							MessageBoxIcon.Error
						);
				};
				worker.RunWorkerAsync();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		private void SaveEstado(object sender, EventArgs e)
		{
			StartWaiting();
			using (BackgroundWorker worker = new BackgroundWorker())
			{
				worker.DoWork += (wSender, wArgs) =>
				{
					IEstadoService estadoService = IoCFactory.Resolve<IEstadoService>();
					Estado.MarkAsModified();
					estadoService.ChangeEstado(Estado);
					Estado.AcceptChanges();
				};
				worker.RunWorkerCompleted += (wSender, wArgs) =>
				{
					EndWaiting();
				};
				worker.RunWorkerAsync();
			}
		}

		#endregion

		#region Event methods

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void SetCurrentEstado(object sender, EventArgs e)
		{
			BindingSource bindingSource = sender as BindingSource;
			if (bindingSource != null)
				if (bindingSource.Current != null)
					Estado = bindingSource.Current as Estado;
		}

		/// <summary>
		/// 
		/// </summary>
		private void OnEstadoPropertyChanged()
		{
			_estadosView.saveButton.Enabled = IsEstadoReady;
			_estadosView.switchActivationButton.Enabled = IsEstadoReady;
			if (IsEstadoActive)
			{
				_estadosView.switchActivationButton.Image = Properties.Resources.delete;
				_estadosView.switchActivationButton.Click += DeactivateEstado;
				_estadosView.switchActivationButton.Text = "Desactivar.";
			}
			else
			{
				_estadosView.switchActivationButton.Image = Properties.Resources.check;
				_estadosView.switchActivationButton.Click += ActivateEstado;
				_estadosView.switchActivationButton.Text = "Activar.";
			}
		}

		/// <summary>
		/// 
		/// </summary>
		private void OnEstadosPropertyChanged()
		{
			_estadosView.estadosBindingSource.DataSource = Estados;
			_estadosView.estadosBindingSource.CurrentChanged += SetCurrentEstado;
			SetCurrentEstado(_estadosView.estadosBindingSource, new EventArgs());
		}

		#endregion

	}
}
